#!/bin/tcsh -ef
####
#
#############################################################################
#                                                                           #
# Title: Calculate FFTs                                                     #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 03/01/2006                                             #
# Last Modification: 03/01/2006                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 10
#
# MANUAL: This script calculates the Fast Fourier Transformations (FFT) of the image, as well as a 2x2 downscaled version of the image, and the image histogram.
#
# MANUAL: It also verifies some parameters and corrects them if needed. <BR><BR>
#
# MANUAL: This script can also generate an average power spectrum for a given image by averaging the power spectrum of a discrete sampling of sub-windows. 
#
# MANUAL: The process generates a final image known as a <B>periodogram</B>.
#
# MANUAL: The number of sub-windows to be averaged can be from <i>0</I> to <i>(Image Side Length - Window Size)</I>, with values beyond this range changed to this maximum. 
#
# MANUAL: The size of the sub-window ranges from <i>0</I> to <i>Image Side Length</I>.
#
# MANUAL: The smaller the window, the faster the program execution; more steps tends toward a more well defined CTF.
#
# PUBLICATION: 2dx - User-friendly image processing for 2D crystals: <A HREF="http://dx.doi.org/10.1016/j.jsb.2006.07.020">J. Struct. Biol. 157 (2007) 64-72</A>
# PUBLICATION: Image processing of 2D crystal images: <A HREF="http://link.springer.com/protocol/10.1007%2F978-1-62703-176-9_10">Methods in Molecular Biology (2012)</A>
#
# DISPLAY: generatePeriodogram
# DISPLAY: periodogramWindowsize
# DISPLAY: periodogramNumsubsteps
# DISPLAY: imagenumber
# DISPLAY: imagename
# DISPLAY: imagename_original
# DISPLAY: nonmaskimagename
# DISPLAY: imagesidelength
# DISPLAY: taperBeforeFFT
# DISPLAY: comment
# DISPLAY: magnification
# DISPLAY: stepdigitizer
# DISPLAY: Calc_from_sample_pixel
# DISPLAY: sample_pixel
# DISPLAY: crop
# DISPLAY: crop_histogram
# DISPLAY: crop_histogram_percent
# DISPLAY: crop_histogram_stdev
# DISPLAY: defocus
# DISPLAY: defocusbackup
# DISPLAY: ctfcor_imode
# DISPLAY: use_masked_image
#
#$end_local_vars
#
set bin_2dx = ""
set proc_2dx = ""
set app_2dx_mrc_converter = ""
#
set PHASEORI_done = ""
#
set tempkeep = ""
set imagename = ""
set nonmaskimagename = ""
set imagenumber = ""
set imagesidelength = ""
set realcell = ""
set magnification = ""
set stepdigitizer = ""
set Calc_from_sample_pixel = ""
set sample_pixel = ""
set realang = ""
set phacon = ""
set RESMIN = ""
set RESMAX = ""
set RADLIM = ""
set generatePeriodogram = ""
set periodogramWindowsize = ""
set periodogramNumsubsteps = ""
set taperBeforeFFT = ""
set crop = ""
set crop_histogram = ""
set crop_histogram_percent = ""
set crop_histogram_stdev = ""
set ctfcor_imode = ""
set use_masked_image = ""
set movie_inmovie = ""
set movie_stackname = ""
#
#$end_vars
#
set scriptname = 2dx_fftrans
set IS_2DX = yes
source ${proc_2dx}/initialize
\rm -f LOGS/${scriptname}.results
#
#
echo "<<@evaluate>>"
#
echo "<<@progress: 1>>"
#
echo "# IMAGE: "${nonmaskimagename}_automask.mrc "<Masking info, automask>" >> LOGS/${scriptname}.results
echo "# IMAGE: "${nonmaskimagename}_manualmask.mrc "<Masking info, manualmask>" >> LOGS/${scriptname}.results
#
#################################################################################
${proc_2dx}/linblock "Sourcing 2dx_init_files_sub.com"
#################################################################################
#
#
#
source ${proc_2dx}/2dx_init_files_sub.com
#
#
#
#################################################################################
${proc_2dx}/linblock "Verifying some parameters"
#################################################################################
#
if ( ! -e ${imagename}.mrc ) then
  ${proc_2dx}/protest "ERROR: ${imagename}.mrc missing. Aborting."
endif
if ( ! -e ${nonmaskimagename}.mrc ) then
  ${proc_2dx}/protest "ERROR: ${nonmaskimagename}.mrc missing. Aborting."
endif
#
set testname = `echo ${imagename} | cut -c2-`
if ((${nonmaskimagename} != ${imagename}) && (${nonmaskimagename} != ${testname})) then
  set oldval = ${nonmaskimagename}
  set nonmaskimagename = ${imagename}
  #############################################################################
  ${proc_2dx}/linblock "Warning: correcting nonmaskimagename from ${oldval} to ${nonmaskimagename}"
  #############################################################################
  echo "set nonmaskimagename = ${nonmaskimagename}"  >> LOGS/${scriptname}.results
endif
# 
if ( ${imagenumber} == "ScriptWillPutNumberHere" || ${imagenumber} == "1000" ) then
  set imagenumber = `echo ${imagename} | ${bin_2dx}/2dx_getnumber.exe`
  #############################################################################
  ${proc_2dx}/linblock "Setting imagenumber to ${imagenumber}"
  #############################################################################
  echo "set imagenumber = ${imagenumber}"  >> LOGS/${scriptname}.results
endif
#
echo "<<@progress: 5>>"
#
echo "# IMAGE-IMPORTANT: "${imagename}.mrc "<Image>" >> LOGS/${scriptname}.results
if ( ${imagename} != ${nonmaskimagename} ) then
  echo "# IMAGE-IMPORTANT: "${nonmaskimagename}.mrc "<Non-masked Image>" >> LOGS/${scriptname}.results
endif
#
if ( ${Calc_from_sample_pixel} == "y" ) then
  set magnification = `echo ${stepdigitizer} ${sample_pixel} | awk '{ s = $1 * 10000.0 / $2 } END { print s }'`
  echo "set magnification = ${magnification}" >> LOGS/${scriptname}.results
  ${proc_2dx}/linblock "Calculating magnification as ${magnification}."
else
  set sample_pixel = `echo ${stepdigitizer} ${magnification} | awk '{ s = $1 * 10000.0 / $2 } END { print s }'`
  echo "set sample_pixel = ${sample_pixel}" >> LOGS/${scriptname}.results
  ${proc_2dx}/linblock "Calculating pixel size on sample level as ${sample_pixel} Angstroems."
endif
#
if ( `echo ${phacon} | awk '{ if (( $1 < 0 ) || ( $1 > 1 )) { s = 1 } else { s = 0 }} END { print s }'` == 1 ) then
  set oldval = ${phacon}
  set phacon = 0.9975
  ${proc_2dx}/linblock "ERROR: correcting phacon from ${oldval} to ${phacon}"
  echo "set phacon = ${phacon}" >> LOGS/${scriptname}.results
endif
#
set testname = `echo ${imagename} | cut -c2-`
if ((${nonmaskimagename} != ${imagename}) && (${nonmaskimagename} != ${testname})) then
  set oldval = ${nonmaskimagename}
  set nonmaskimagename = ${imagename}
  ${proc_2dx}/linblock "ERROR: correcting nonmaskimagename from ${oldval} to ${nonmaskimagename}"
  echo "set nonmaskimagename = ${nonmaskimagename}" >> LOGS/${scriptname}.results
endif
# 
echo "<<@progress: 10>>"
#
if ( `echo ${RESMIN} ${RESMAX} | awk '{ if ( $1 < $2 ) { s = 1 } else { s = 0 }} END { print s }'` == 1 ) then
  set oldval = ${RESMIN}
  set RESMIN = ${RESMAX}
  set RESMAX = ${oldval}
  ${proc_2dx}/linblock "ERROR: exchanging RESMIN and RESMAX, to RESMIN=${RESMIN}, and RESMAX=${RESMAX}"
  echo "set RESMIN = ${RESMIN}" >> LOGS/${scriptname}.results
  echo "set RESMAX = ${RESMAX}" >> LOGS/${scriptname}.results
endif
#
set testval1 = `echo ${RADLIM} | cut -d\, -f1`
set testval2 = `echo ${RADLIM} | cut -d\, -f2`
set testval3 = `echo ${RADLIM} | cut -d\, -f3`
set correctit = 0
#
if ( `echo ${testval1} | awk '{ if (( $1 < 0 ) || ( $1 > 50 )) { s = 1 } else { s = 0 }} END { print s }'` == 1 ) then
  set oldval = ${testval1}
  set testval1 = 35
  set correctit = 1 
  ${proc_2dx}/linblock "ERROR: correcting RADLIM first value from ${oldval} to ${testval1}"
endif
#
if ( `echo ${testval2} | awk '{ if (( $1 < 0 ) || ( $1 > 50 )) { s = 1 } else { s = 0 }} END { print s }'` == 1 ) then
  set oldval = ${testval2}
  set testval2 = 35
  set correctit = 1 
  ${proc_2dx}/linblock "ERROR: correcting RADLIM second value from ${oldval} to ${testval2}"
endif
#
if ( ${correctit} == 1 ) then
  set RADLIM = `echo ${testval1},${testval2},${testval3}`
  echo "set RADLIM = ${RADLIM}" >> LOGS/${scriptname}.results
endif
#
set newimagenumber = `echo ${imagenumber} | ${bin_2dx}/2dx_getnumber.exe`
if ( ${newimagenumber} != ${imagenumber} ) then
  ${proc_2dx}/linblock "WARNING: correcting imagenumber from ${imagenumber} to ${newimagenumber}"
  set imagenumber = ${newimagenumber}
  echo "set imagenumber = ${imagenumber}"  >> LOGS/${scriptname}.results
endif
#
set testval = `echo ${imagenumber} | wc -c`
if ( `echo ${testval} | awk '{ if ( $1 < 11 ) { s = 1 } else { s = 0 }} END { print s }'` == 1 ) then
  set oldval = ${imagenumber}
  set imagenumber = `echo 0000000000 | cut -c${testval}-`${imagenumber}
  ${proc_2dx}/linblock "ERROR: correcting imagenumber from ${oldval} to ${imagenumber}"
  echo "set imagenumber = ${imagenumber}" >> LOGS/${scriptname}.results
endif  
#
if ( `echo ${testval} | awk '{ if ( $1 > 11 ) { s = 1 } else { s = 0 }} END { print s }'` == 1 ) then
  set oldval = ${imagenumber}
  set startnum = `echo ${testval} | awk '{ s = $1 - 10 } END { print s }'`
  set endnum   = `echo ${testval} | awk '{ s = $1 - 1 } END { print s }'`
  set imagenumber = `echo ${imagenumber} | cut -c${startnum}-${endnum}`
  ${proc_2dx}/linblock "ERROR: correcting imagenumber from ${oldval} to ${imagenumber}"
  echo "set imagenumber = ${imagenumber}" >> LOGS/${scriptname}.results
endif  
#
echo "<<@progress: 5>>"
#
#############################################################################
#############################################################################
#######  Now Source 2dx_fftrans_sub.com  ####################################
#############################################################################
#############################################################################
#
#################################################################################
${proc_2dx}/linblock "Sourcing 2dx_fftrans_sub.com"
#################################################################################
#
set inimage = ${imagename}
source ${proc_2dx}/2dx_fftrans_sub.com
#
#################################################################################
${proc_2dx}/linblock "Back in 2dx_fftrans.com"
#################################################################################
#
if ( -l movie_aligned.mrc ) then
  \rm -f movie_aligned.mrc
endif
if ( ! -e movie_aligned.mrc ) then
  \ln -s ${inimage}.mrc movie_aligned.mrc
endif
if ( ${movie_stackname} == "ScriptWillPutNameHere" ) then
  echo "set movie_stackname = movie_aligned" >> LOGS/${scriptname}.results
endif
#
\rm -f movie_aligned.mrc.png
${app_2dx_mrc_converter} --size 400 ${inimage}.mrc movie_aligned.mrc.png
#
if ( ! -e movie_aligned_fft.mrc ) then
  \rm -f movie_aligned_fft.mrc
  \ln -s FFTIR/${inimage}_fft.mrc movie_aligned_fft.mrc
endif
#
\rm -f movie_aligned_fft.mrc.png
${app_2dx_mrc_converter} --size 400 FFTIR/${inimage}_fft.mrc movie_aligned_fft.mrc.png
#
echo "set FFT_done = y" >> LOGS/${scriptname}.results
echo "<<@evaluate>>"
#
##########################################################################
${proc_2dx}/linblock "${scriptname} - normal end."
##########################################################################
##
#
echo "<<@progress: 100>>"
exit
#
# These are listed here to make sure they appear in the 2dx_image GUI:
source ${proc_2dx}/2dx_initialize_tiff_to_mrc_sub.com
source ${proc_2dx}/2dx_initialize_test_endianess_of_mrc_sub.com
source ${proc_2dx}/2dx_initialize_make_image_square_sub.com
source ${proc_2dx}/2dx_initialize_crop_histogram_sub.com
source ${proc_2dx}/2dx_correctHeaderCell_sub.com
python ${proc_2dx}/movie/mask.py
#
